//
//  ChangeLinkResultCell.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/15.
//

import UIKit

class ChangeLinkResultCell: UITableViewCell {

    // 图片
    @IBOutlet weak var goodsImg: UIImageView!
    // 名称
    @IBOutlet weak var goodsName: UILabel!
    // 原价
    @IBOutlet weak var oldPrice: UILabel!
    // 券后价
    @IBOutlet weak var subPrice: UILabel!
    // 优惠券
    @IBOutlet weak var coupon: UILabel!
    // 返利
    @IBOutlet weak var refund: UILabel!
    // 销量
    @IBOutlet weak var sales: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        oldPrice.zm.addDeleteLine("#999999".zm.hex())
        
        let priceStr = subPrice.text!
        let attrString = NSMutableAttributedString(string: priceStr)
        if let r = priceStr.range(of: "¥") {
            let range = NSRange(r,in:priceStr)
            attrString.addAttributes([.font: UIFont.zm.fontSemibold(size: 12)], range: range)
        }
        if let r = priceStr.range(of: "券后") {
            let range = NSRange(r,in:priceStr)
            attrString.addAttributes([.font: UIFont.zm.fontSemibold(size: 12)], range: range)
        }
        subPrice.attributedText = attrString
    }
    
    func setUI(_ info: TBGoods) {
        goodsImg.zm.setImage(urlStr: info.mainPic)
        goodsName.text = info.title
        let handel = ZMUtil.DecimalNumberHandler(roundingMode: .down, scale: 2)
        let commissionMoney = NSDecimalNumber.init(value: info.commissionRate).dividing(by: NSDecimalNumber.init(value: 100)).multiplying(by: NSDecimalNumber.init(value: info.actualPrice)).rounding(accordingToBehavior: handel)
        refund.text = "\(commissionMoney.stringValue)元"
        coupon.text = info.couponPrice.zm.toString(keep: 2) + "元"
        oldPrice.text = "原价￥\(info.originalPrice.zm.toString(keep: 2))"
        subPrice.text = "¥\(info.actualPrice.zm.toString(keep: 2)) 券后"
        sales.text = "销量: \(info.monthSales)"
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
